#include "CapitalMarketCNEX.h"
#include "CapitalMarketCNEXForm.h"
#include "../FIDataCenter.h"
#include <QPainter>
#include <QMouseEvent>
#include <qmath.h>
#include <QLayout>
#include <widgets/SImageButton.h>
#include <core/StyleMgr.h>

CapitalMarketCNEX::CapitalMarketCNEX(const QString& strTitle, QWidget *parent)
    : SDialog(parent)
{
    setTitle(strTitle);

    //setMouseTracking(true);
    //setAttribute(Qt::WA_Hover,true);
    chartForm = new CapitalMarketCNEXForm(this);
    chartForm->setStyleSheet(
        QString("CapitalMarketCNEXForm {"
        "  background-color: #%3;" //rgb(35, 38, 40)
        "}"
        ""
        "CapitalMarketCNEXForm QLabel {"
        "  color: #%1;" //rgb(190, 198, 201)
        "}"
        ""
        "CapitalMarketCNEXForm QCheckBox {"
        "  background-color: #%4;" //rgb(53,57,59)
        "  color: #%2;" //rgb(255,235,200)
        "}"
        "CapitalMarketCNEXForm QCheckBox::unchecked {"
        "  color: grey;"
        "}").arg(StyleMgr::instance().color("TitleWord").rgb(), 0, 16)
            .arg(StyleMgr::instance().color("ChartH8").rgb(), 0, 16)
            .arg(StyleMgr::instance().color("ElementBackground").rgb(), 0, 16)
            .arg(StyleMgr::instance().color("ButtonDisable").rgb(), 0, 16)
        );

    QLayout* layout = this->layout();
    layout->addWidget(chartForm);

    connect(&FIDataCenter::instance(), &FIDataCenter::recvCNEX, this, &CapitalMarketCNEX::onCNEXDataArrived);

    FIDataCenter::instance().reqCNEX();
}

CapitalMarketCNEX::~CapitalMarketCNEX()
{
}

bool CapitalMarketCNEX::is_unfold() const
{
    return chartForm && chartForm->isVisible();
}

void CapitalMarketCNEX::fold()
{
    if (chartForm)
        chartForm->hide();
}

void CapitalMarketCNEX::unfold()
{
    if (chartForm)
        chartForm->show();
}

void CapitalMarketCNEX::onCNEXDataArrived()
{
    if (chartForm) {
        chartForm->clearData();
        const QVector<FICNEXData::tCNEXData>& data = FIDataCenter::instance().m_dataCNEX.getData();
        for (int i=0; i<data.size(); ++i) {
            char heights[4] = {
                (char)data[i].AllMarket,
                (char)data[i].BigBank,
                (char)data[i].SmallBank,
                (char)data[i].NotBank
            };
            chartForm->setData(i, heights);
        }
    }
}

SDialog *CapitalMarketCNEX::clone(QWidget *parent)
{
    CapitalMarketCNEX* pWidget = new CapitalMarketCNEX(m_strTitle, parent);
    return pWidget;
}

